package com.cxd.app.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cxd.app.entity.base.UserTokenBase;
import com.cxd.app.service.LoanService;
import com.cxd.app.service.UserService;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.controller.BaseController;
import com.cxd.base.util.BeanTools;

/**
 * 网贷之家接口
 * @author wtj
 * @date 2016年4月22日 下午2:13:41
 * @company CXD
 * @version [Copyright (c) 2016 V001]
 */
@Controller("WdzjController")
@RequestMapping("")
public class WdzjController extends BaseController{
	private static final Logger logger = Logger.getLogger(WdzjController.class);
	
	@Resource
	public LoanService loanService;
	
	@Resource
	public UserService userService;
	
	@Resource
	public BeanTools bt;
	
	/**
	 * 网贷之家获取借款信息
	 * @author wtj
	 * @date 2016年4月25日下午1:36:16
	 * @param response
	 * @param date
	 * @param page
	 * @param pageSize
	 * @throws IOException
	 */
	@RequestMapping(value = "wdzj_loans", method = {RequestMethod.GET,RequestMethod.POST })
	public void wdzj_loans(HttpServletResponse response,String date,String page,String pageSize,String token)throws IOException {
		String logInfo = String.format("时间:%s,当前页:%s,页大小:%s,TOKEN:%s",date,page,pageSize,token);
		super.requestLog("wdzj_loans",logInfo);
		JSONObject json = new JSONObject();
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("pageSize", pageSize);
		param.put("currentPage", page);
		param.put("date", date);
		try {
			UserTokenBase utBean = bt.queryBean("USER_TOKEN", "token", token.toString(), UserTokenBase.class);
			if(utBean != null && (utBean.getCtime().getTime()+3600*1000)>new Date().getTime()){
				loanService.wdzj_loans(json, param);
			}else{
				json.put("msg", "token错误,请重新登录");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,logInfo);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json.toJSONString().replace("null", "''"));
	}
	
	@RequestMapping(value = "wdzj_login", method = {RequestMethod.GET,RequestMethod.POST })
	public void wdzj_login(HttpServletResponse response,String username,String password)throws IOException {
		String logInfo = String.format("用户名:%s,密码:%s",username,password);
		super.requestLog("wdzj_login",logInfo);
		JSONObject json = new JSONObject();
		try {
			userService.toLogin(json, username, password);
			if(json.getString("status").equals("200")){
				Map<String, Object> param = new HashMap<String,Object>();
				param.put("token", json.getObject("result", Map.class).get("token"));
				json.put("data", param);
				json.remove("result");
				json.remove("msg");
				json.remove("status");
			}
		} catch(CxdException e){
			super.cxdJsonException(json, e,logInfo);
		}catch (Exception e) {
			super.jsonException(json, e, logger);
		}
		super.responseSendMsg(response, json.toJSONString().replace("null", "''"));
	}
}
